﻿var config = {
    userStore: new Oidc.WebStorageStateStore({ store: window.localStorage }),
    authority: 'http://localhost:5000',
    client_id: 'IdentityServer4Client',
    redirect_uri: 'http://localhost:5000/account/callback',
    scope: 'openid IdentityServer4Api custom',
    response_type: 'code',
    post_logout_redirect_uri: 'http://localhost:5000',
    automaticSilentRenew: true,
    silent_redirect_uri: 'http://localhost:5000/account/refresh'
}
var mgr = new Oidc.UserManager(config)
window.login = function () {
    mgr.signinRedirect();
}
window.logout = function () {
    this.console.log("logout");
    mgr.signoutRedirect();
}
window.getUser = async function () {
    try {
        var a = await mgr.getUser();
        this.console.log(a);
        return a
    } catch (e) {
        this.console.log(e);
        return e
    }
}
window.callback = async function () {
    try {
        this.console.log("1231");
        var a = await mgr.signinRedirectCallback();
        this.console.log(a);
        return true
    } catch (e) {
        this.console.log(e);
        return false
    }
}
window.refresh = async function () {
    try {
        this.console.log("refresh11231231");
      return  await mgr.signinSilentCallback();
    } catch (e) {
        this.console.log("refresh");
        this.console.log(e);
        return e
    }
}
mgr.events.addUserLoaded(function (user) {
});
mgr.events.addUserUnloaded(function () {
    console.log("unload" + new Date());
});
mgr.events.addAccessTokenExpiring(function () {
    console.log("Access token expiring..." + new Date());
    mgr.removeUser();
    mgr.signinRedirect();
});
mgr.events.addSilentRenewError(function (err) {
    console.log(err);
    mgr.signinRedirect();
});
mgr.events.addUserSignedOut(function () {
    mgr.removeUser();
});